package com.cat.dataStructure02;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/longest-happy-string/description/
 * @create 2025/10/29 21:22
 * @since JDK17
 */

public class Solution83 {
    class Pair {
        int freq;
        char c;
        public Pair(int freq, char c) {
            this.freq = freq;
            this.c = c;
        }
    }
    public String longestDiverseString(int a, int b, int c) {
        Pair[] arr = new Pair[]{new Pair(a, 'a'), new Pair(b, 'b'), new Pair(c, 'c')};
        StringBuilder ans = new StringBuilder();
        while (true) {
            Arrays.sort(arr, (x, y) -> y.freq - x.freq);
            boolean haveVal = false;
            for (var x : arr) {
                if (x.freq <= 0) {  // 后面的也比然小于0
                    break;
                }
                int n = ans.length();
                if (n >= 2 && ans.charAt(n - 1) == ans.charAt(n - 2) && ans.charAt(n - 1) == x.c) { // 用不了
                    continue;
                }
                haveVal = true;
                x.freq--;
                ans.append(x.c);
                break;
            }
            if (!haveVal) {
                break;
            }
        }

        return ans.toString();
    }
}
